<template>
  <div class="app-container">
    <div class="chart-wrapper">
      <bar-chart
        :bar-line="barLine"
        :bar-data="barData"
        :avg-data="avgData"
        :bar-type="'number'"
      />
    </div>
    <el-divder></el-divder>
    <div class="filter-container">
      <el-switch
        class="filter-item"
        v-model="listQuery.status"
        active-value="enable"
        inactive-value="disable"
        active-text="正常"
        inactive-text="黑名单"
        @change="getList"
      ></el-switch>
      <el-radio-group v-model="listQuery.is_vip" @change="getList" class="filter-item" style="float:right;margin-right: 10px;margin-top:10px">
        <el-radio label="">全部</el-radio>
        <el-radio :label="0">普通</el-radio>
        <el-radio :label="1">会员</el-radio>
      </el-radio-group>
      <el-input
        class="filter-item"
        placeholder="输入关键字进行过滤"
        v-model="listQuery.keyword"
        style="width: 15%;margin-left:10px"
      >
      </el-input>
      <!--      <el-input v-model="listQuery.title" placeholder="Title" style="width: 200px;" class="filter-item"-->
      <!--                @keyup.enter.native="handleFilter"-->
      <!--      />-->
      <!--      <el-select v-model="listQuery.importance" placeholder="Imp" clearable style="width: 90px" class="filter-item">-->
      <!--        <el-option v-for="item in importanceOptions" :key="item" :label="item" :value="item" />-->
      <!--      </el-select>-->
      <!--      <el-select v-model="listQuery.type" placeholder="Type" clearable class="filter-item" style="width: 130px">-->
      <!--        <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name+'('+item.key+')'" :value="item.key" />-->
      <!--      </el-select>-->
      <!--      <el-select v-model="listQuery.sort" style="width: 140px" class="filter-item" @change="handleFilter">-->
      <!--        <el-option v-for="item in sortOptions" :key="item.key" :label="item.label" :value="item.key" />-->
      <!--      </el-select>-->
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="getList"
                 style="margin-left: 10px"
      >
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit"
                 @click="handleCreate"
      >
        添加用户
      </el-button>
      <el-button class="filter-item" type="success" @click="get_ocpc"
                 style="margin-left: 10px"
      >
        联调测试
      </el-button>
      <el-radio-group v-model="listQuery.Login_type" @change="getList" class="filter-item" style="margin-left: 10px">
        <el-radio-button label="0">全部</el-radio-button>
        <el-radio-button label="2">手机号</el-radio-button>
        <el-radio-button label="1">微信</el-radio-button>
      </el-radio-group>
<!--      <el-button size="mini" type="success" round style="margin-left:10px;" @click="handleClickExport">-->
<!--        导出手机号-->
<!--      </el-button>-->
      <el-date-picker
        class="filter-item"
        style="margin-left:10px;"
        v-model="listQuery.time"
        type="datetimerange"
        value-format="yyyy-MM-dd HH:mm:ss"
        :picker-options="pickerOptions"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        @change="getList"
        align="right">
      </el-date-picker>
      <!--      <el-button v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download"-->
      <!--                 @click="handleDownload"-->
      <!--      >-->
      <!--        导出-->
      <!--      </el-button>-->
      <!--      <el-checkbox v-model="showReviewer" class="filter-item" style="margin-left:15px;" @change="tableKey=tableKey+1">-->
      <!--        审核-->
      <!--      </el-checkbox>-->
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" min-width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="50" align="center" label="注册类型">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.Login_type == 1">微信</el-tag>
          <el-tag type="primary" v-else>手机号</el-tag>
        </template>
      </el-table-column>
      <el-table-column min-width="80" align="center" label="用户名">
        <template slot-scope="scope">
          <span>{{ scope.row.nickname }}</span>
          <!--          <span>{{ scope.row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>-->
        </template>
      </el-table-column>
      <el-table-column min-width="80" align="center" label="手机号">
        <template slot-scope="scope">
          <span>{{ scope.row.mobile }}</span>
          <!--          <span>{{ scope.row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>-->
        </template>
      </el-table-column>
      <el-table-column min-width="80" align="center" label="微信id">
        <template slot-scope="scope">
          <span>{{ scope.row.openid }}</span>
          <!--          <span>{{ scope.row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>-->
        </template>
      </el-table-column>
      <el-table-column min-width="80" align="center" label="来源">
        <template slot-scope="scope">
          <el-link type="primary" v-if="scope.row.bd_vid">百度推广</el-link>
          <el-link type="success" v-else>自然流量</el-link>
        </template>
      </el-table-column>
      <el-table-column min-width="80" align="center" label="会员状态">
        <template slot-scope="scope">
          <el-link type="success" v-if="scope.row.is_vip == 1">会员</el-link>
          <el-link type="info" v-else>普通</el-link>
        </template>
      </el-table-column>

      <el-table-column min-width="100" align="center" label="到期时间">
        <template slot-scope="scope">
          <span>{{ scope.row.vip_time}}</span>
        </template>
      </el-table-column>


      <el-table-column min-width="100" align="center" label="最后登陆">
        <template slot-scope="scope">
          <span>{{ scope.row.lastlogintime}}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="50" align="center" label="禁用">
        <template slot-scope="scope">
          <el-switch
            class="filter-item"
            v-model="scope.row.status"
            active-value="enable"
            inactive-value="disable"
            @change="changeStatus($event,scope.row.id)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="60">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" circle @click="handleUpdate(scope.row)">
          </el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" circle @click="del(scope.row.id)">
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="getList"
    />
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="40%">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="120px"
               style="width: 80%; margin-left:10%;"
      >
        <el-form-item label="会员状态">
          <el-radio-group v-model="temp.is_vip">
            <el-radio :label="0">普通</el-radio>
            <el-radio :label="1">会员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="到期时间" v-if="temp.is_vip == 1">
          <el-date-picker
            v-model="temp.vip_time"
            type="datetime"
            default-time="12:00:00"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="每日下载数量" v-if="temp.is_vip == 1">
          <el-input v-model="down_count" maxlength="11" placeholder="需要改动时修改即可" style="width:70%"/>
        </el-form-item>

        <el-form-item label="手机号">
          <el-input v-model="temp.mobile" maxlength="11" type="number" style="width:70%"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <center>
          <el-button @click="resetForm">
            取消
          </el-button>
          <el-button type="primary" @click="dialogStatus==='create'?createData():createData()">
            提交
          </el-button>
        </center>
      </div>
    </el-dialog>

    <el-dialog title="联调测试" :visible.sync="dialogFormVisible1" width="40%">
      <el-form ref="ocpcForm"  :model="ocpc_temp" label-position="left" label-width="120px"
               style="width: 80%; margin-left:10%;"
      >

        <el-form-item label="类型">
          <el-input v-model="ocpc_temp.type" maxlength="11" placeholder="类型" style="width:70%"/>
        </el-form-item>

        <el-form-item label="bd_vid">
          <el-input v-model="ocpc_temp.bd_vid"  style="width:70%"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <center>
          <el-button @click="resetForm1">
            取消
          </el-button>
          <el-button type="primary" @click="dialogStatus==='create'?createOcpc():createOcpc()">
            提交
          </el-button>
        </center>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, create, del, changeStatus,create_ocpc } from '@/api/member'
// import { getMenus } from '@/api/menu'
import Pagination from '@/components/Pagination'
import store from '@/store'
import router from '@/router' // Secondary package based on el-pagination
import ImageCropper from '@/components/ImageCropper'
import PanThumb from '@/components/PanThumb'
import { getMonth_user,  } from '@/api/main'
import BarChart from './components/BarChart'

export default {
  name: 'MemberList',
  components: { Pagination, ImageCropper, PanThumb, BarChart},
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      barLine:[],
      barData:[],
      avgData:[],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      imagecropperShow: false,
      imagecropperKey: 0,
      menus: [],
      roles: [],
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        keyword: '',
        status:'enable',
        is_vip:'',
        time:[],
        Login_type:0,
      },
      temp: {
        id: undefined,
        mobile:'',
        is_vip:1,
        vip_time:''
      },
      ocpc_temp: {
        type:'',
        bd_vid:''
      },
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      filterText: '',
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      down_count:'',
      rules: {
        real_name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        id_card: [{ required: true, message: '请填写身份证', trigger: 'blur' }],
        mobile: [{ required: true, message: '请填写手机号', trigger: 'blur' }],
        // password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.getList()
    this.getMonth()
  },
  methods: {
    handleClickExport() {
      // if (this.checkedIds.length < 1) {
      //   this.$message('您尚未选中要导出的数据，请先选择')
      //   return
      // }
      let search = JSON.stringify(this.listQuery)
      window.open('/api/excel/export_member?search='+search)
      // downMembers(this.checkedIds).then(response => {
      //
      // })
    },
    getMonth() {
      getMonth_user().then(response => {
        this.barLine =response.data.line
        this.barData =response.data.data
      })
    },
    get_ocpc(){
      this.dialogFormVisible1 = true;
      this.ocpc_temp.type =''
      this.ocpc_temp.bd_vid=''
    },

    cropSuccess(resData) {
      this.imagecropperShow = false
      this.imagecropperKey = this.imagecropperKey + 1
      this.temp.avatar = resData.url
    },
    close() {
      this.imagecropperShow = false
    },
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    getCheckedNodes() {
      console.log(this.$refs.tree.getCheckedNodes())
    },
    setCheckedNodes(row) {
      this.$refs.tree.setCheckedNodes(row)
    },
    getList() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      })
    },
    resetForm() {
      this.dialogFormVisible = false
    },
    resetForm1() {
      this.dialogFormVisible1 = false
    },
    changeStatus(val, id) {
      changeStatus(val, id).then(response => {
        this.$notify.success({
          title: '消息提示',
          message: response.msg,
          offset: 100
        })
      })
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.temp.down_count = this.down_count
          create(this.temp).then(response => {
            // this.list.unshift(this.temp)
            this.dialogFormVisible = false
            if (response.code == 200) {
              this.$notify.success({
                title: '成功',
                message: response.msg,
                type: 'success',
                duration: 2000
              })
              this.resetTemp()
              this.getList()
            } else {
              this.$notify.error({
                title: '失败',
                message: response.msg,
                duration: 2000
              })
            }
          })
        }
      })
    },
    createOcpc() {
          if(!this.ocpc_temp.type){
            this.$message.info('类型不能为空')
            return ''
          }
          if(!this.ocpc_temp.bd_vid){
            this.$message.info('bd_vid不能为空')
            return ''
          }
          create_ocpc(this.ocpc_temp.type,this.ocpc_temp.bd_vid).then(response => {
            // this.list.unshift(this.temp)
            this.dialogFormVisible1 = false
            if (response.code == 200) {
              this.$notify.success({
                title: '成功',
                message: response.msg,
                type: 'success',
                duration: 2000
              })
            } else {
              this.$notify.error({
                title: '失败',
                message: response.msg,
                duration: 2000
              })
            }
          })
    },
    handleUpdate(row) {
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.temp.id = row.id // copy obj
      this.temp.is_vip = row.is_vip
      this.temp.vip_time = row.vip_time
      this.temp.mobile = row.mobile
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    del(id) {
      this.$confirm('删除用户后可能会导致个别用户关联数据失效。确认要删除该条数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(id).then(response => {
          // this.list.unshift(this.temp)
          if (response.code == 200) {
            this.$notify.success({
              title: '成功',
              message: response.msg,
              type: 'success',
              duration: 2000
            })
            this.resetTemp()
            this.getList()
          } else {
            this.$notify.error({
              title: '失败',
              message: response.msg,
              duration: 2000
            })
          }
        })
      }).catch(() => {

      })
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        mobile:'',
        is_vip:1,
        vip_time:''
      }
      this.down_count = ''
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}

.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
<style scoped>
.avatar {
  width: 200px;
  height: 200px;
  border-radius: 50%;
}
</style>
